package Entity;
 

 
import Controller.Client;
 
import java.util.LinkedList;
 
import java.util.Date;
 

 
/**
 
 * This is an entity for subscription. 
 
 * it has two subclass for casual subscription and for full subscription.
 
 * @author Or Marx
 
 *
 
 */
 
public class Subscription implements Cloneable
 
{
 
        /**
 
         * is a field that will be like "time stamp" in order to make the subscription number unique for every subscriber.
 
         */
 
        private static int next_sub_num=1;      //used to know which sub num is taken and which can be used
 
        private String subscription_number;
 
        private String customer_id;
 
        private String car_number;
 
        private Date start_date;
 
        private Date end_date;
 
        /**
 
         * is an enum for errors. we can get one of 3 enums:
 
         * CORRECT= if everything is ok.
 
         * ID_ERROR= error in the id.
 
         * CAR_NUMBER_ERROR= car number error.
 
         */
 
        public enum subOrderEnum {CORRECT,ID_ERROR,CAR_NUMBER_ERROR};
 
        
 
        public Subscription(String num,String id,String car,Date s, Date e)
 
        {
 
                this.start_date=new Date(s.getTime());
 
                this.end_date=new Date(e.getTime());
 
                this.car_number=new String(car);
 
                this.customer_id=new String(id);
 
                this.subscription_number=new String(num);
 
        }
 
        
 
        /**
 
         * is the constructor. 
 
         * it sets the start date and end date.
 
         * the end date will be from 28 days from the start date.
 
         * it also sets the next_sub_num for this subscription. 
 
         */
 
        public Subscription()
 
        {
 
                this.start_date=new Date();
 
                this.end_date=new Date();
 
                this.end_date.setTime(this.end_date.getTime()+1000*60*60*24*28);
 
                this.subscription_number=String.valueOf(Subscription.next_sub_num);
 
                Subscription.next_sub_num++;
 
        }
 
        // set and get sub_num.
 
        public String getSubscription_number() {
 
                return subscription_number;
 
        }
 
        public void setSubscription_number(String subscription_number) {
 
                this.subscription_number = subscription_number;
 
        }
 
        // set and get start_date.
 
        public Date getStart_date() {
 
                return start_date;
 
        }
 
        public void setStart_date(Date start_date) {
 
                this.start_date = start_date;
 
        }
 
        // set and get car_num.
 
        public String getCar_number() {
 
                return car_number;
 
        }
 
        public void setCar_number(String car_number) {
 
                this.car_number = car_number;
 
        }
 
        // set and get customer_id.
 
        public String getCustomer_id() {
 
                return customer_id;
 
        }
 
        public void setCustomer_id(String customer_id) {
 
                this.customer_id = customer_id;
 
        }
 
        /**
 
         * is the function for cloning the subscription.
 
         * it copy the whole fields and also take care of the next_sub_num not to be increased.
 
         */
 
        public Subscription clone()
 
        {
 
                Subscription sub=new Subscription();
 
                Subscription.next_sub_num--;
 
                sub.subscription_number=this.subscription_number;
 
                sub.customer_id=this.customer_id;
 
                sub.car_number=this.car_number;
 
                sub.start_date=this.start_date;
 
                return sub;
 
        }
 
        
 
        /**
 
         * is a validation function. 
 
         * it checks the id length and the car number length.
 
         * @return
 
         */
 
        public subOrderEnum validate()
 
        {
 
                if (this.customer_id.length() != 9)
 
                        return subOrderEnum.ID_ERROR;
 
                try{
 
                          Integer.parseInt(this.customer_id);
 
                          // is an integer!
 
                        } catch (NumberFormatException e) {
 
                                return subOrderEnum.ID_ERROR;// not an integer!
 
                        }
 
                
 
                if (this.car_number.length() != 7)
 
                        return subOrderEnum.CAR_NUMBER_ERROR;
 
                try{
 
                          Integer.parseInt(this.car_number);
 
                          // is an integer!
 
                        } catch (NumberFormatException e) {
 
                                return subOrderEnum.CAR_NUMBER_ERROR;// not an integer!
 
                        }
 
                
 
                return subOrderEnum.CORRECT;    
 
        }
 
        
 
        
 
        public void save()
 
        {
 
                Client.saveSubscriptionToDB((Object)this);
 
        }
 
        
 
        public String toString ()
 
        {
 
                return("costumer id:  "+this.getCustomer_id()+"\ncar number:  "+this.getCar_number()+"\nsubscription number:  "+this.getSubscription_number()+"\ndate:"+this.getStart_date().toString());
 
        }
 

 
        // set and get end_date.
 
        public Date getEnd_date() {
 
                return end_date;
 
        }
 
        public void setEnd_date(Date end_date) {
 
                this.end_date = end_date;
 
        }
 
        
 
}
 
